/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.Register.Logic;

import Data.DAO.ICustomerStore;
import data.pojo.Collection;
import data.pojo.Customer;
import data.pojo.Order;
import java.util.ArrayList;

/**
 *
 * @author LiuBole
 */
public class RegisterLogic {

    //private ICustomerStore customerDS = new ICustomerStore();

    public int nameIsValid(String name) { //长度不合法返回0，存在同名返回-1，完全合法返回1；
        boolean lengthIsValid = (name.length() >= 2) && (name.length() <= 12);
        if(lengthIsValid){
            ICustomerStore customerDS = new ICustomerStore();
            if(customerDS.searchObjectsByAttr("name", name).isEmpty()){
                return 1;
            } else {
                return -1;
            }
        } else {
            return 0;
        }
    }

    public boolean codeIsValid(String code, String codeReInput) {
        boolean isValid = (code.length() >= 6) && (code.length() <= 20);
        boolean isEquals = code.equals(codeReInput);
        return isValid && isEquals;
    }
    
    private String creatCustomerID(){
        ICustomerStore customerDS = new ICustomerStore();
        return String.valueOf(customerDS.getObjects().size()+1);
    }

    public String addNewCustomer(String name, String code, String reInputCode) {
        boolean nameIsValid = (nameIsValid(name)==1);
        boolean codeIsValid = codeIsValid(code, reInputCode);
        if (nameIsValid && codeIsValid) {
            ICustomerStore customerDS = new ICustomerStore();
            String userID = creatCustomerID();
            ArrayList<Customer> newList = new ArrayList();
            //初始化顾客信息
            Customer customer = new Customer();
            customer.setId(userID);
            customer.setCollections(new ArrayList<Collection>());
            customer.setName(name);
            customer.setOrderList(new ArrayList<Order>());
            customer.setPaasword(code);
            customer.setPoints(0);
            
            newList.add(customer);
            customerDS.add(newList);
            return userID;
        } else {
            return null;
        }

    }
}
